package people

import (
	"context"
	"github.com/flipped-aurora/gin-vue-admin/server/global"
	"github.com/flipped-aurora/gin-vue-admin/server/model/people"
	peopleReq "github.com/flipped-aurora/gin-vue-admin/server/model/people/request"
)

type UsersService struct{}

// CreateUsers 创建users表记录
// Author [yourname](https://github.com/yourname)
func (usersService *UsersService) CreateUsers(ctx context.Context, users *people.Users) (err error) {
	err = global.GVA_DB.Create(users).Error
	return err
}

// DeleteUsers 删除users表记录
// Author [yourname](https://github.com/yourname)
func (usersService *UsersService) DeleteUsers(ctx context.Context, ID string) (err error) {
	err = global.GVA_DB.Delete(&people.Users{}, "id = ?", ID).Error
	return err
}

// DeleteUsersByIds 批量删除users表记录
// Author [yourname](https://github.com/yourname)
func (usersService *UsersService) DeleteUsersByIds(ctx context.Context, IDs []string) (err error) {
	err = global.GVA_DB.Delete(&[]people.Users{}, "id in ?", IDs).Error
	return err
}

// UpdateUsers 更新users表记录
// Author [yourname](https://github.com/yourname)
func (usersService *UsersService) UpdateUsers(ctx context.Context, users people.Users) (err error) {
	err = global.GVA_DB.Model(&people.Users{}).Where("id = ?", users.ID).Updates(&users).Error
	return err
}

/*
// GetUsers 根据ID获取users表记录
// Author [yourname](https://github.com/yourname)

	func (usersService *UsersService) GetUsers(ctx context.Context, ID string) (users people.Users, err error) {
		err = global.GVA_DB.Where("id = ?", ID).First(&users).Error
		return
	}

// GetUsersInfoList 分页获取users表记录
// Author [yourname](https://github.com/yourname)

	func (usersService *UsersService) GetUsersInfoList(ctx context.Context, info peopleReq.UsersSearch) (list []people.Users, total int64, err error) {
		limit := info.PageSize
		offset := info.PageSize * (info.Page - 1)
		// 创建db
		db := global.GVA_DB.Model(&people.Users{})
		var userss []people.Users
		// 如果有条件搜索 下方会自动创建搜索语句
		if info.StartCreatedAt != nil && info.EndCreatedAt != nil {
			db = db.Where("created_at BETWEEN ? AND ?", info.StartCreatedAt, info.EndCreatedAt)
		}

		if info.Name != nil && *info.Name != "" {
			db = db.Where("name LIKE ?", "%"+*info.Name+"%")
		}
		if info.Phone != nil && *info.Phone != "" {
			db = db.Where("phone LIKE ?", "%"+*info.Phone+"%")
		}
		if info.Email != nil && *info.Email != "" {
			db = db.Where("email LIKE ?", "%"+*info.Email+"%")
		}
		err = db.Count(&total).Error
		if err != nil {
			return
		}

		if limit != 0 {
			db = db.Limit(limit).Offset(offset)
		}

		err = db.Find(&userss).Error
		return userss, total, err
	}
*/

func (usersService *UsersService) GetUsersPublic(ctx context.Context) {
	// 此方法为获取数据源定义的数据
	// 请自行实现
}

type UsersGoods struct {
	GoodsId   int     `json:"goods_id" form:"goods_id" gorm:"comment:商品id;column:goods_id;"`       //商品id
	UsersName string  `json:"users_name" form:"users_name" gorm:"comment:用户名;column:users_name;"`  //用户名
	Img       string  `json:"img" form:"img" gorm:"comment:用户头像;column:img;"`                      //用户头像
	GoodsName string  `json:"goods_name" form:"goods_name" gorm:"comment:商品名称;column:goods_name;"` //商品名称
	Price     float64 `json:"price" form:"price" gorm:"comment:商品价格;column:price;"`                //商品价格
}

// GetUsers 根据ID获取users表记录
// Author [yourname](https://github.com/yourname)
func (usersService *UsersService) GetUsers(ctx context.Context, ID string) (users UsersGoods, err error) {
	err = global.GVA_DB.Model(&people.Goods{}).Select("goods.id goods_id,users.name users_name,users.img,goods.name goods_name,goods.price").Joins("JOIN users ON users.id=goods.user_id").Where("users.id = ?", ID).First(&users).Error
	return
}

// GetUsersInfoList 分页获取users表记录
// Author [yourname](https://github.com/yourname)
func (usersService *UsersService) GetUsersInfoList(ctx context.Context, info peopleReq.UsersSearch) (list []UsersGoods, total int64, err error) {
	limit := info.PageSize
	offset := info.PageSize * (info.Page - 1)
	// 创建db
	db := global.GVA_DB.Model(&people.Goods{}).Select("goods.id goods_id,users.name users_name,users.img,goods.name goods_name,goods.price").Joins("JOIN users ON users.id=goods.user_id")
	//var userss []people.Users
	// 如果有条件搜索 下方会自动创建搜索语句
	if info.StartCreatedAt != nil && info.EndCreatedAt != nil {
		db = db.Where("created_at BETWEEN ? AND ?", info.StartCreatedAt, info.EndCreatedAt)
	}

	if info.Name != nil && *info.Name != "" {
		db = db.Where("name LIKE ?", "%"+*info.Name+"%")
	}
	if info.Phone != nil && *info.Phone != "" {
		db = db.Where("phone LIKE ?", "%"+*info.Phone+"%")
	}
	if info.Email != nil && *info.Email != "" {
		db = db.Where("email LIKE ?", "%"+*info.Email+"%")
	}
	err = db.Count(&total).Error
	if err != nil {
		return
	}

	if limit != 0 {
		db = db.Limit(limit).Offset(offset)
	}

	err = db.Find(&list).Error
	return list, total, err
}
